package org.phalanx.client.mapeditor;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

import org.phalanx.client.UI.PhalanxIconButton;
import org.phalanx.client.UI.swing.listeners.pButtonListener;
import org.phalanx.client.UI.swing.listeners.pButtonPressed;
import org.phalanx.client.menus.PhalanxSideToolBarAction;

public class PhalanxEditorSideToolBarPanel extends JPanel implements
		pButtonListener {
	private PhalanxMapEditorPanel masterPanel;
	private PhalanxIconButton selection;
	private PhalanxIconButton stamp;
	private PhalanxIconButton deleteTile;
	private PhalanxIconButton addBuildPoint;
	private PhalanxIconButton removeBuildPoint;
	private ArrayList<PhalanxIconButton> buttonList;
	private PhalanxIconButton zoomIn;
	private PhalanxIconButton zoomOut;
	private PhalanxIconButton navigation;
	private PhalanxIconButton selectedButton;
	private PhalanxSideToolBarAction action;
	private JPanel gridPanel;

	public PhalanxEditorSideToolBarPanel(PhalanxMapEditorPanel parentPanel) {
		this.masterPanel = parentPanel;
		initializePanel();
	}

	private void initializePanel() {
		this.setBackground(new Color(80, 80, 80));
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		buttonList = new ArrayList<PhalanxIconButton>();
		createComponents();
		createGridPanel();

		this.add(gridPanel);
	}

	private void createGridPanel() {
		gridPanel = new JPanel();
		gridPanel.setLayout(new GridLayout(0, 3));
		gridPanel.setBackground(new Color(80, 80, 80));

		for (int i = 0; i < buttonList.size(); i++){
			gridPanel.add(buttonList.get(i));
		}
		/*gridPanel.add(selection);
		gridPanel.add(stamp);
		gridPanel.add(deleteTile);
		gridPanel.add(addBuildPoint);*/
	}

	private void createComponents() {
		try {
			action = new PhalanxSideToolBarAction(masterPanel);

			BufferedImage selectionImg = ImageIO.read(new File(
					"./resources/images/toolbars/SelectAction.png"));
			BufferedImage stampImg = ImageIO.read(new File(
					"./resources/images/toolbars/StampTile.png"));
			BufferedImage deleteTileImg = ImageIO.read(new File(
					"./resources/images/toolbars/DeleteTile.png"));
			BufferedImage addBuildPointImg = ImageIO.read(new File(
					"./resources/images/toolbars/AddBuildPoint.png"));
			BufferedImage removeBuildPointImg = ImageIO.read(new File(
			"./resources/images/toolbars/RemoveBuildPoint.png"));
			BufferedImage zoomInImg = ImageIO.read(new File(
			"./resources/images/toolbars/ZoomInButton.png"));
			BufferedImage zoomOutImg = ImageIO.read(new File(
			"./resources/images/toolbars/ZoomOutButton.png"));
			BufferedImage navImg = ImageIO.read(new File(
			"./resources/images/toolbars/Navigation.png"));
			
			selection = new PhalanxIconButton();
			selection
					.setDefaultIcon(new ImageIcon(createBevel(selectionImg, 4)));
			selection.setHoveredIcon(selection
					.getIcon(PhalanxIconButton.DEFAULT_ICON));
			selection.setClickedIcon(new ImageIcon(lightenImage(createBevel(
					selectionImg, 2))));
			selection.setToolTipText("Selection mode");
			selection.setName("selection");

			stamp = new PhalanxIconButton();
			stamp.setDefaultIcon(new ImageIcon(createBevel(stampImg, 4)));
			stamp.setHoveredIcon(stamp.getIcon(PhalanxIconButton.DEFAULT_ICON));
			stamp.setClickedIcon(new ImageIcon(lightenImage(createBevel(
					stampImg, 2))));
			stamp.setToolTipText("Write tile");
			stamp.setName("stamp");

			deleteTile = new PhalanxIconButton();
			deleteTile.setDefaultIcon(new ImageIcon(createBevel(deleteTileImg,
					4)));
			deleteTile.setHoveredIcon(deleteTile
					.getIcon(PhalanxIconButton.DEFAULT_ICON));
			deleteTile.setClickedIcon(new ImageIcon(lightenImage(createBevel(
					deleteTileImg, 2))));
			deleteTile.setToolTipText("Remove tile");
			deleteTile.setName("remove");
			
			addBuildPoint = new PhalanxIconButton();
			addBuildPoint.setDefaultIcon(new ImageIcon(createBevel(addBuildPointImg,
					4)));
			addBuildPoint.setHoveredIcon(addBuildPoint
					.getIcon(PhalanxIconButton.DEFAULT_ICON));
			addBuildPoint.setClickedIcon(new ImageIcon(lightenImage(createBevel(
					addBuildPointImg, 2))));
			addBuildPoint.setToolTipText("Add BuildPoint");
			addBuildPoint.setName("addbp");
			
			removeBuildPoint = new PhalanxIconButton();
			removeBuildPoint.setDefaultIcon(new ImageIcon(createBevel(removeBuildPointImg,
					4)));
			removeBuildPoint.setHoveredIcon(removeBuildPoint
					.getIcon(PhalanxIconButton.DEFAULT_ICON));
			removeBuildPoint.setClickedIcon(new ImageIcon(lightenImage(createBevel(
					removeBuildPointImg, 2))));
			removeBuildPoint.setToolTipText("Delete BuildPoint");
			removeBuildPoint.setName("removebp");
			
			zoomIn = new PhalanxIconButton();
			zoomIn.setDefaultIcon(new ImageIcon(createBevel(zoomInImg,
					4)));
			zoomIn.setHoveredIcon(zoomIn
					.getIcon(PhalanxIconButton.DEFAULT_ICON));
			zoomIn.setClickedIcon(new ImageIcon(lightenImage(createBevel(
					zoomInImg, 2))));
			zoomIn.setToolTipText("Zoom In Map");
			zoomIn.setName("zoomin");
			
			zoomOut = new PhalanxIconButton();
			zoomOut.setDefaultIcon(new ImageIcon(createBevel(zoomOutImg,
					4)));
			zoomOut.setHoveredIcon(zoomOut
					.getIcon(PhalanxIconButton.DEFAULT_ICON));
			zoomOut.setClickedIcon(new ImageIcon(lightenImage(createBevel(
					zoomOutImg, 2))));
			zoomOut.setToolTipText("Zoom Out Map");
			zoomOut.setName("zoomout");
			
			navigation = new PhalanxIconButton();
			navigation.setDefaultIcon(new ImageIcon(createBevel(navImg,
					4)));
			navigation.setHoveredIcon(navigation
					.getIcon(PhalanxIconButton.DEFAULT_ICON));
			navigation.setClickedIcon(new ImageIcon(lightenImage(createBevel(
					navImg, 2))));
			navigation.setToolTipText("Navigate Map");
			navigation.setName("nav");

			buttonList.add(selection);
			buttonList.add(stamp);
			buttonList.add(deleteTile);
			buttonList.add(addBuildPoint);
			buttonList.add(removeBuildPoint);
			buttonList.add(zoomIn);
			buttonList.add(zoomOut);
			buttonList.add(navigation);

			for (int i = 0; i < buttonList.size(); i++) {
				buttonList.get(i).addButtonListener(this);
				buttonList.get(i).addButtonListener(action);
			}

		} catch (IOException ie) {
		}
	}

	public PhalanxIconButton getButton(String refName) {
		PhalanxIconButton selectedButton = null;

		for (int i = 0; i < buttonList.size() && selectedButton == null; i++) {
			if (buttonList.get(i).getName().equals(refName)) {
				selectedButton = buttonList.get(i);
			}
		}

		return selectedButton;
	}

	private BufferedImage createBevel(BufferedImage newImg, int thickness) {
		BufferedImage bevelImg = new BufferedImage(newImg.getWidth(), newImg
				.getHeight(), BufferedImage.TYPE_INT_RGB);

		bevelImg.setData(newImg.getData());

		Integer pX = bevelImg.getWidth();
		Integer pY = bevelImg.getHeight();

		Graphics2D gfx = (Graphics2D) bevelImg.getGraphics();
		Color origColor = gfx.getColor();

		gfx.setColor(Color.black);

		for (int i = 0; i < thickness + 1; i++) {
			gfx.drawLine(pX - i, 0, pX - i, pY);
		}

		for (int i = 0; i < thickness + 1; i++) {
			gfx.drawLine(0, pY - i, pX, pY - i);
		}

		gfx.setColor(origColor);
		return bevelImg;
	}

	private BufferedImage lightenImage(BufferedImage newImg) {
		BufferedImage lightImage;

		RescaleOp r = new RescaleOp(1.3f, 0, null);
		lightImage = r.filter(newImg, null);

		return lightImage;
	}

	public void actionPerformed(pButtonPressed p) {
		applyButtonAction((PhalanxIconButton) p.getComponent());
	}

	public void applyButtonAction(PhalanxIconButton p) {
		flipButtonState(selectedButton);
		selectedButton = p;
		flipButtonState(selectedButton);
	}

	private void flipButtonState(PhalanxIconButton p) {
		if (p != null) {
			Icon clickedIcon = p.getIcon(PhalanxIconButton.CLICKED_ICON);
			Icon unclickedIcon = p.getIcon(PhalanxIconButton.DEFAULT_ICON);

			p.setDefaultIcon(clickedIcon);
			p.setHoveredIcon(clickedIcon);
			p.setClickedIcon(unclickedIcon);
		}
	}

	public PhalanxSideToolBarAction getToolbarAction() {
		return action;
	}
}
